/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.FlowerListDAO;
import DAO.ViewDetailDAO;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import xmlobject.FlowerList.FlowerAtt;

/**
 *
 * @author ADMIN
 */
public class SearchFlower extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = (String) request.getParameter("action");
            if (action.equals("SearchByCategory")) {
                String realpath = getServletContext().getRealPath("/");
                int categoryId = Integer.parseInt(request.getParameter("id"));
                try {
                    FlowerListDAO.FlowerListtoXML(realpath, categoryId);
                    out.print("Success");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
                } catch (JAXBException ex) {
                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (action.equals("ViewDetail")) {
                String realpath = getServletContext().getRealPath("/");
                int categoryId = Integer.parseInt(request.getParameter("id"));
                try {
                    ViewDetailDAO.ViewDetailtoXML(realpath, categoryId);
                    out.print("SuccessDetail");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
                } catch (JAXBException ex) {
                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
                }
//            }
//            else if (action.equals("SeachByName")) {
//                String realpath = getServletContext().getRealPath("/");
//                String productName = request.getParameter("Txt-Search");
//                try {
//                    SearchFlowerDAO.SearchFlowertoXML(realpath, productName);
//                    String url = "SearchFlower.jsp";
//                    RequestDispatcher rd = request.getRequestDispatcher(url);
//                    rd.forward(request, response);
//                } catch (ClassNotFoundException ex) {
//                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (SQLException ex) {
//                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (JAXBException ex) {
//                    Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
//                }
            } else if (action.equals("SeachByName")) {
                String name = request.getParameter("Txt-Search");
                XMLInputFactory xif = XMLInputFactory.newInstance();
                String fileName = getServletContext().getRealPath("/")
                        + "XML/LoadAllFlowers.xml";

                XMLStreamReader xsr = xif.createXMLStreamReader(fileName, new FileInputStream(fileName));
                int eventType = xsr.getEventType();
                String productId = null;
                String productName = null;
                String price = null;
                String image = null;
                String productUnit = null;
                ArrayList list = new ArrayList();
                boolean found = false;
                while (xsr.hasNext()) {
                    eventType = xsr.next();
                    if(eventType == XMLStreamConstants.START_ELEMENT){
                        if(xsr.getLocalName().equals("ProductId")){
                            eventType=xsr.next();
                            productId = xsr.getText();
                        }else if(xsr.getLocalName().equals("ProductName")){
                            eventType = xsr.next();
                            productName = xsr.getText();
                            if(productName.trim().indexOf(name)>-1){
                                found = true;
                            }
                        }else if(found && xsr.getLocalName().equals("Price")){
                            eventType = xsr.next();
                            price = xsr.getText();
                        }else if(found && xsr.getLocalName().equals("Image")){
                            eventType = xsr.next();
                            image = xsr.getText();
                        }else if(found && xsr.getLocalName().equals("ProductUnit")){
                            eventType = xsr.next();
                            productUnit = xsr.getText();
                            FlowerAtt flower = new FlowerAtt(Integer.parseInt(productId), productName, Integer.parseInt(price), image, Integer.parseInt(productUnit));
                            list.add(flower);
                            found = false;
                        }
                    }
                }
                request.setAttribute("INFO", list);
                RequestDispatcher rd = request.getRequestDispatcher("SearchFlower.jsp");
                rd.forward(request, response);
            }


        } catch (XMLStreamException ex) {
            Logger.getLogger(SearchFlower.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
